package com.iking.toolkit.system.controller;

import cn.hutool.core.util.StrUtil;

import com.iking.toolkit.model.Msg;
import com.iking.toolkit.system.model.http.HttpDictionaryTypeSelectReq;
import com.iking.toolkit.system.model.model.DictionaryType;
import com.iking.toolkit.system.service.DictionaryTypeService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


/**
 * 数据字典类型管理
 * @author wk
 * @date 7/2/2020 上午9:49
 */
@RestController
@RequestMapping("/sys/dictionaryType")
@Tag(name = "DictionaryTypeController", description = "数据字典类型管理")
@Slf4j
public class DictionaryTypeController {

    /**
     * 数据字典类型管理
     */
    @Autowired
    private DictionaryTypeService dictionaryTypeService;

    /**
     * @Description: 查询全部数据字典类型
     * @Param: [req]
     * @return: com.iking.common.model.Msg
     * @Author: wk
     * @Date: 7/2/2020 上午9:49
     */
    @Operation(summary = "查询全部数据字典类型")
    @RequestMapping(value = "/get/all", method = RequestMethod.POST)
    public Msg searchDictionaryType(@RequestBody HttpDictionaryTypeSelectReq req) {
        try {
            if (req != null && req.getTypeName() != null) {
                req.setTypeName(req.getTypeName().replaceAll(" ", ""));
            }
            return dictionaryTypeService.searchDictionaryType(req);
        } catch (Exception e) {
            log.error("查询全部类型异常,异常信息：" + e);
            return Msg.fail("查询全部类型异常");
        }
    }

    /**
     * @Description: 新增数据字典类型
     * @Param: [req]
     * @return: com.iking.common.model.Msg
     * @Author: wk
     * @Date: 7/2/2020 上午9:52
     */
    @Operation(summary = "新增数据字典类型")
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public Msg insertDictionaryType(@RequestBody DictionaryType req) {
        try {
            if (req == null) {
                return Msg.fail("新增内容为空");
            }
            if (StringUtils.isBlank(req.getDictionaryTypeName())) {
                return Msg.fail("类型名称为空");
            }else{
                if(req.getDictionaryTypeName().length() > 50){
                    return Msg.fail("参数最大可以输入50个字符");
                }
            }
            req.setDictionaryTypeName(req.getDictionaryTypeName().trim());

            if (StringUtils.isBlank(req.getTypePym())) {
                return Msg.fail("拼音码为空");
            }else{
                if(req.getTypePym().length() > 50){
                    return Msg.fail("参数最大可以输入50个字符");
                }
            }
            if(StrUtil.isNotBlank(req.getDescription()) && req.getDescription().length() > 200){
                return Msg.fail("参数说明最大可以输入200个字符");
            }
            req.setTypePym(req.getTypePym().trim());
            if (req.getIsSysDefault() == null || "".equals(req.getIsSysDefault().toString().replaceAll(" ", ""))) {
                req.setIsSysDefault(false);
            }
            return dictionaryTypeService.insert(req);
        } catch (Exception e) {
            log.error("新增数据字典类型异常");
            return Msg.fail("新增数据字典类型异常，异常原因：" + e);
        }
    }

    /**
     * @Description: 修改数据字典类型
     * @Param: [req]
     * @return: com.iking.common.model.Msg
     * @Author: wk
     * @Date: 7/2/2020 上午9:53
     */
    @Operation(summary = "修改数据字典类型")
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public Msg updateDictionaryType(@RequestBody DictionaryType req) {
        try {
            if (req == null) {
                return Msg.fail("修改内容为空");
            }
            //fid非空校验
            if (StringUtils.isBlank(req.getDictionaryTypeId())){
                return Msg.fail("fid不能为空！");
            }
            if (StringUtils.isBlank(req.getDictionaryTypeName())) {
                return Msg.fail("类型名称为空");
            }else{
                if(req.getDictionaryTypeName().length() > 50){
                    return Msg.fail("参数最大可以输入50个字符");
                }
            }
            if (StringUtils.isBlank(req.getTypePym())) {
                return Msg.fail("拼音码为空");
            }else{
                if(req.getTypePym().length() > 50){
                    return Msg.fail("参数最大可以输入50个字符");
                }
            }
            if(StrUtil.isNotBlank(req.getDescription()) && req.getDescription().length() > 200){
                return Msg.fail("参数说明最大可以输入200个字符");
            }
            if (req.getDictionaryTypeName() != null && "".equals(req.getDictionaryTypeName().replaceAll(" ", ""))) {
                req.setDictionaryTypeName(null);
            }
            if (req.getTypePym() != null && "".equals(req.getTypePym().replaceAll(" ", ""))) {
                req.setTypePym(null);
            }
            if (req.getIsSysDefault() != null && "".equals(req.getIsSysDefault().toString().replaceAll(" ", ""))) {
                req.setIsSysDefault(null);
            }
            if (req.getIsDelete() != null && "".equals(req.getIsDelete().toString().replaceAll(" ", ""))) {
                req.setIsDelete(null);
            }
            if (req.getIsEnable() != null && "".equals(req.getIsEnable().toString().replaceAll(" ", ""))) {
                req.setIsEnable(null);
            }
            return dictionaryTypeService.update(req);
        } catch (Exception e) {
            log.error("修改数据字典类型异常，异常信息：" + e);
            return Msg.fail("修改数据字典类型异常");
        }
    }

    /**
     * @Description: 删除数据字典类型
     * @Param: [id]
     * @return: com.iking.common.model.Msg
     * @Author: wk
     * @Date: 7/2/2020 上午9:54
     */
    @Operation(summary = "删除数据字典类型")
    @RequestMapping(value = "/delete/{id}", method = RequestMethod.POST)
    public Msg deleteByID(@PathVariable("id") String id) {
        try {
            if (StringUtils.isBlank(id)) {
                return Msg.fail("数据字典类型ID为空");
            }
            //执行删除
            return dictionaryTypeService.deleteByPrimaryKey(id);
        } catch (Exception e) {
            log.error("删除数据字典类型异常");
            return Msg.fail("删除数据字典类型异常，异常原因：" + e);
        }
    }
}
